package com.jm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jm.common.util.entity.R;
import com.jm.common.util.entity.SelectCondition;
import com.jm.pojo.Toy;
import com.jm.pojo.dto.ToyDto;
import com.jm.service.ToyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/toy")
public class ToyController {
    @Autowired
    private ToyService toyService;

    /**
     * 分页条件查询
     */
    @GetMapping("/page")
    @Cacheable(value = "ToyCache", key = "#result.data")
    public R<IPage<ToyDto>> selectByPageCondition(SelectCondition condition) {
//        分页查询
        IPage<Toy> toyIPage = toyService.selectByPageCondition(condition);
//        将查询到的列表转化为Toy的扩展对象
        IPage<ToyDto> toyDtoIPage = toyService.toDto(toyIPage);
        return R.success(toyDtoIPage);
    }

    /**
     * 添加一个
     */
    @PostMapping
    @CacheEvict(value = "ToyCache",allEntries = true)
    public R<String> addOne(@RequestBody Toy toy) {
        boolean save = toyService.save(toy);
        if (save) return R.success("添加成功");
        return R.error("添加失败");
    }

    /**
     * 修改
     */
    @PutMapping
    @CacheEvict(value = "ToyCache",allEntries = true)
    public R<String> update(@RequestBody Toy toy) {
        boolean result = toyService.updateById(toy);
        if (result) return R.success("修改成功");
        return R.error("修改失败");
    }

    /**
     * 批量/单个状态修改
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "ToyCache",allEntries = true)
    public R<String> changeToyStatus(@PathVariable Integer status,@RequestParam("ids") List<String> ids) {
        boolean b = toyService.changeStatusByIds(ids, status);
        if (b) return R.success("状态修改操作成功");
        return R.error("状态修改操作失败");
    }

    /**
     * 批量/单个删除
     */
    @DeleteMapping()
    @CacheEvict(value = "ToyCache",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        boolean result = toyService.removeBatchByIds(ids);
        if (result) return R.success("删除操作成功");
        return R.error("删除操作失败");
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/{id}")
    @Cacheable(value = "ToyCache",key = "#id")
    public R<Toy> selectById(@PathVariable String id) {
        Toy toy = toyService.getById(Long.parseLong(id));
        if (toy != null) return R.success(toy);
        return R.error("没有寻找到对应的单品，请重试");
    }

    @GetMapping("/list")
    @Cacheable(value = "ToyCache",key = "#categoryId")
    public R<List<Toy>> getByCategoryId(String categoryId) {
        List<Toy> toyList = toyService.getByCategoryId(categoryId);
        if (toyList == null || toyList.size() == 0) return R.error("未查询到合适的玩具列表");
        return R.success(toyList);
    }
}
